package com.likai.tag;

import com.likai.dao.IJarMapper;
import com.likai.dao.ITagMapper;
import com.likai.index.JarsIndex;
import com.likai.pojo.JarBean;
import com.likai.pojo.TagBean;
import com.likai.util.SqlSessionFactoryUtil;
import org.apache.ibatis.session.SqlSession;
import org.apache.log4j.Logger;

import java.util.List;
import java.util.UUID;
import java.util.regex.Pattern;

/**
 * Created by likai on 2018/10/14.
 * 生成jar包标签
 */
public class JarsTag {
    //获取日志对象
    private static Logger logger = Logger.getLogger(JarsTag.class) ;

    public static void main(String [] args) {
        logger.info("创建标签开始");

        //实例化sqlSession
        SqlSession sqlSession = SqlSessionFactoryUtil.openSqlSession() ;
        //实例化接口jarMapper
        IJarMapper jarMapper = sqlSession.getMapper(IJarMapper.class) ;
        //实例化接口tagMapper
        ITagMapper tagMapper = sqlSession.getMapper(ITagMapper.class) ;


        //查询所有tagstate为0的数据
        List<JarBean> jarList = jarMapper.selectInfoByTagState() ;
        for (JarBean jarBean : jarList) {
            String id = jarBean.getUuid() ;
            String name = jarBean.getName() ;
            //通过name进行拆分标签
            String [] tags = name.replaceAll(".jar","").split("-") ;
            for (String tag : tags) {
                //符合仅包含字母和数字的正则
                if(tag.matches("[0-9a-zA-Z]{0,15}")) {
                    try {
                        //查询此tag是否存在
                        TagBean bean = tagMapper.selectTagByName(tag) ;
                        if(bean == null) {
                            TagBean po = new TagBean(UUID.randomUUID().toString(),tag);
                            tagMapper.insertDynamic(po) ;

                        }
                    } catch (Exception e) {
                        logger.error("Exception",e);
                    }

                }
            }
            //更新t_jar中的tagstate
            jarBean.setTagState(1);
            jarMapper.updateDynamic(jarBean) ;

            sqlSession.commit();
        }

        try{
            sqlSession.close();
        } catch (Exception e) {
            logger.error("Exception",e);
        }


        logger.info("创建标签结束");
    }

}
